﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using SSO.Model;
using Vici.CoolStorage;
using System.Collections;
using System.Reflection;

namespace SSO
{
    /// <summary>
    /// SSO服务中心
    /// </summary>
    [WebService(Namespace = "HyiSSO")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class ServiceCenter : System.Web.Services.WebService
    {

        [WebMethod(Description = "注册一个新的令牌")]
        public string AddNewToken(string username, string userPWd, string ip, string SiteAdd)
        {
            Check check = Check.New();
            check.User = username;
            check.UserPwd = userPWd;
            check.IP = ip;
            check.Token = System.Guid.NewGuid().ToString();
            check.SiteGroup = 0;
            check.State = true;
            check.CreateDate = DateTime.Now;
            check.Save();
            return check.Token;
        }
        [WebMethod(Description = "检查改用户是否处于登录中")]
        public string CheckToken(string ToKen, string ip)
        {
            Check check = Check.ReadFirst("Token=@Token", "@Token", ToKen);
            if (check != null)
                if (check.IP == ip)
                    if (check.State != false)
                        return check.User;
            return null;
        }
        [WebMethod(Description = "注销用户")]
        public bool LoginOut(string ToKen)
        {
            Check check = Check.ReadFirst("Token=@Token", "@Token", ToKen);
            if (check != null)
            {
                check.State = false;
                check.Save();
                return true;
            }
            return false;
        }
        [WebMethod(Description = "获取网站列表")]
        public DictionaryEntry[] SiteList(string group)
        {
            CSList<Site> site = SSO.Model.Site.List("Group=@Group", "@Group", "0");
            Hashtable sitelist = new Hashtable();
            foreach (Site s in site)
            {
                sitelist.Add(s.SiteName, s.SiteAdd);
            }
            DictionaryEntry[] array = new DictionaryEntry[sitelist.Count];
            sitelist.CopyTo(array, 0);
            return array;
        }
        [WebMethod(Description = "测试匿名对象")]
        public DictionaryEntry[] test()
        {
            object obj = new { LoginID = 1, RealName = "ss" };
            Hashtable temp = new Hashtable();
            foreach (PropertyInfo o in obj.GetType().GetProperties())
            {
                temp.Add(o.Name, o.GetValue(obj, null));   
            }
            DictionaryEntry[] array = new DictionaryEntry[temp.Count];
            temp.CopyTo(array, 0);
            return array;
        }

    }
}
